package wdt_qf.job;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import wdt_qf.dao.AuditingDao;
import wdt_qf.dao.ZxyNeedauditingretailsbillidDao;
import wdt_qf.entity.wdt.WdtHttpResult;
import wdt_qf.entity.wdt.WdtResponseJson;
import wdt_qf.service.WdtService;

import javax.annotation.Resource;
import java.util.List;

@Component
@EnableScheduling
@EnableAsync
@Slf4j
public class UploadBill {
    @Resource
    private WdtService wdtService;
    @Resource
    private ZxyNeedauditingretailsbillidDao zxyNeedauditingretailsbillidDao;
    @Resource
    private AuditingDao auditingDao;

    @Scheduled(fixedDelay = 20000)  //间隔1秒1000
    public void uploadBuyBill() {
        //上传之前先删除冲红的单据
        zxyNeedauditingretailsbillidDao.deleteRedWordBill();
        List<String> needUploadbill = zxyNeedauditingretailsbillidDao.getNeedUploadbillByBillType(34);
        for (String billCode : needUploadbill) {
            WdtResponseJson wdtResponseJson = wdtService.uploadOrderBuyToWdt(billCode);
            log.info("自动上传采购订单返回结果集" + JSON.toJSONString(billCode + "单据上传采购订单到旺店通返回结果为:" + JSON.toJSONString(wdtResponseJson)));
            WdtResponseJson wdtResponseJson2 = wdtService.uploadBuyToWdt(billCode);
            log.info("自动上传采购订单返回结果集" + JSON.toJSONString(billCode + "单据上传采购订单到旺店通返回结果为:" + JSON.toJSONString(wdtResponseJson2)));
            if (wdtResponseJson2.getCode() == 0) {
                auditingDao.BuyBillAutoConversionPsBillByBillCode(billCode);
            }
            zxyNeedauditingretailsbillidDao.updateNeedUploadbill(billCode);
        }
    }

    @Scheduled(fixedDelay = 20000)  //间隔1秒1000
    public void uploadBuyBackBill() {
        //上传之前先删除冲红的单据
        zxyNeedauditingretailsbillidDao.deleteRedWordBill();
        List<String> needUploadbill = zxyNeedauditingretailsbillidDao.getNeedUploadbillByBillType(6);
        for (String billCode : needUploadbill) {
            WdtResponseJson wdtResponseJson = wdtService.uploadCGTHToWdt(billCode);
            log.info("自动上传采购退货返回结果集" + JSON.toJSONString(billCode + "单据上传采购退货单到旺店通返回结果为:" + JSON.toJSONString(wdtResponseJson)));
            zxyNeedauditingretailsbillidDao.updateNeedUploadbill(billCode);
        }
    }
}
